import socket
import time
import sys
from threading import Thread, currentThread, activeCount


def receive(conn):
    print("receive-------------------------------:\n")
    try:
        while True:
            ret = str(conn.recv(1024), encoding="utf-8")
            print("receive:" + ret.strip(" "))
    except Exception as e:
        print("链接断开", e)
    finally:
        conn.close()


def send(conn):
    while True:
        from pip._vendor.distlib.compat import raw_input
        line = raw_input()
        if line.__contains__(","):
            print(line)
            conn.send(bytes(line.strip("\n").strip(" "), encoding="utf-8"))


if __name__ == "__main__":
    conn = socket.socket()
    conn.connect(("127.0.0.1", 1234))

    # t1 = Thread(target=send, args=(conn,))
    t2 = Thread(target=receive, args=(conn,))
    t2.start()
    time.sleep(2)
    for line in sys.stdin:
        print(line)
        conn.send(bytes(line.strip("\n").strip(" "), encoding="utf-8"))
